package com.sensePlusVR.main;

import android.annotation.SuppressLint;
import android.graphics.SurfaceTexture;
import android.os.Bundle;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.realGame.VideoGamePlayer;
import com.sensePlusVR.utils.AppUtil;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.PageJumpUtil;
import com.senseplusvr.sdk.videoview.HemiGLView;
import com.senseplusvr.sdk.videoview.SvrConstant;
import com.senseplusvr.sdk.videoview.SvrInterf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import com.example.awsdemo.BluetoothPlayer.BTDepth;

@SuppressLint({ "InlinedApi", "NewApi" })
@SuppressWarnings("unused")
public class VrRealTestActivity extends VrRealTestBaseActivity {
	private static final String TAG = "VrRealTestActivity";
	private VideoGamePlayer mPlayer;
	private HemiGLView mRenderView;
	private SurfaceTexture nativeTexture;

	private Object mFadeLock;
	private int mPlayMode;
	private List<String> mConfigList;
	private int mCurrentConfig;
	private static final int PLAYER_START = 0;
	private static final int PLAYER_ACTION = 1;
	private static final int PLAYER_END = 2;
	private static final int PLAYER_DES = 3;
	private static final int VR_180_mono = 1;
	private static final int VR_180_lf   = 2;
	private static final int VR_360_mono = 3;
	private static final int VR_360_lf   = 4;
	private static final int NORMAL_mono     = 5;
	private static final int NORMAL_lf       = 6;
	private int mCurrentPlayer;
	private float value = 0;
	private final float alpha = 0.49f;
	private boolean mRenderViewLocked = false;
	private boolean mFading = false;
	private boolean mIsInterActiveMode = false;
	private boolean mIsWorking = false;
	private boolean mIsPaused = false;
	
	private Map<String, String> mapStrings;
	
	private boolean isViewMoved = false;
	
	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
		AppUtil.hideVirtualKey(this);
		init();
		initDetectData();
//		AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//		mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
//				mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) - 2,
//				AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
	}
	

	
	private void initDetectData() {
		mapStrings = new HashMap<String, String>();
		
	}



	private void init() {
		mFadeLock = new Object();

		mCurrentConfig = 0;
		mCurrentPlayer = PLAYER_START;

		mPlayer = new VideoGamePlayer(this, getActions());
		//mPlayer.create(mConfigList.get(mCurrentConfig));
		mPlayer.setGameEventListener(mGameCallBack);

		mRenderView = getHemiGlView();

		int sourcetype = getVrVideo()?VR_180_mono:NORMAL_mono;
		
		MyLog.e(TAG, "sourcetype ： " + sourcetype);
		
		if (sourcetype == VR_180_mono)
			mRenderView.setInputVideoType(SvrConstant.InputVideoType.InputVideo_MONO);
		else if (sourcetype == VR_180_lf)
			mRenderView.setInputVideoType(SvrConstant.InputVideoType.InputVideo_LR);
		
		
		mRenderView.setDrawFrameCallback(new SvrInterf.DrawFrameCallback() {

			@Override
			public void onFrameUpdate() {
				// TODO Auto-generated method stub
				// Log.e(TAG, "update "+System.currentTimeMillis());
				mPlayer.showActionFrame();
			}

		});
	}

	@Override
	public void onSurfaceTexture(SurfaceTexture surfacetexture) {
		// TODO Auto-generated method stub
		super.onSurfaceTexture(surfacetexture);
		if (surfacetexture == null){
			MyLog.e(TAG, "surfacetexture==null");
			saveResult(0, 0 ,false);
			boolean vrMode = getVrMode();
			MyLog.d(TAG, "vrMode : " + vrMode);
			if(vrMode){
				finish();
				PageJumpUtil.JumpToRealGameForTest(VrRealTestActivity.this, !vrMode, true,getIsFromSetting());
			}
			return;
		}
		MyLog.e(TAG, "surface=" + surfacetexture);
		nativeTexture = surfacetexture;
		mPlayer.setPlayerSurface(nativeTexture);
		mPlayer.PlayTest();// start to play
		mIsWorking = true;
		mRenderView.pauseTracking();
	}

	private void saveResult(int dfps, int gfps, boolean result){
		if (getVrVideo()){
			MySharedPreferences.save(VRApplication.getAppContext(), Constant.DECODER_VR_RESULT,Boolean.valueOf(result));
			MySharedPreferences.save(VRApplication.getAppContext(), Constant.DECODER_VR_FPS,Integer.valueOf(dfps));
			if (getVrMode())
				MySharedPreferences.save(VRApplication.getAppContext(), Constant.GPU_VR_FPS,Integer.valueOf(gfps));
			else
				MySharedPreferences.save(VRApplication.getAppContext(), Constant.GPU_VR2D_FPS,Integer.valueOf(gfps));
		}else{
			MySharedPreferences.save(VRApplication.getAppContext(), Constant.DECODER_2D_RESULT,Boolean.valueOf(result));
			MySharedPreferences.save(VRApplication.getAppContext(), Constant.DECODER_2D_FPS,Integer.valueOf(dfps));
			MySharedPreferences.save(VRApplication.getAppContext(), Constant.GPU_2D_FPS,Integer.valueOf(gfps));
		}
		
		MyLog.d(TAG, "getVrVideo ： " + getVrVideo() + " getVrMode : " + getVrMode());
		if(getVrVideo()){
			if(!getVrMode()){
				boolean decoderVrResult = MySharedPreferences.getBoolean(VRApplication.getAppContext(), Constant.DECODER_VR_RESULT);
				int decodeVrFps = MySharedPreferences.getInteger(VRApplication.getAppContext(), Constant.DECODER_VR_FPS);
				int gpuVrFps = MySharedPreferences.getInteger(VRApplication.getAppContext(), Constant.GPU_VR_FPS);
				int gpuVr2dFps = MySharedPreferences.getInteger(VRApplication.getAppContext(), Constant.GPU_VR2D_FPS);
				
				boolean hasAccel = vrApplication.HasAccelSensor();
				boolean hasGrav = vrApplication.HasGravSensor();
				boolean hasGyro = vrApplication.HasGyroSensor();
				
				mapStrings.put(getResources().getString(R.string.has_accel_sensor), String.valueOf(hasAccel));
				mapStrings.put(getResources().getString(R.string.has_grav_sensor), String.valueOf(hasGrav));
				mapStrings.put(getResources().getString(R.string.has_gyro_sensor), String.valueOf(hasGyro));
				mapStrings.put(getResources().getString(R.string.decoder_vr_result), String.valueOf(decoderVrResult));
				mapStrings.put(getResources().getString(R.string.decoder_vr_fps), String.valueOf(decodeVrFps));
				mapStrings.put(getResources().getString(R.string.gpu_vr_fps), String.valueOf(gpuVrFps));
				mapStrings.put(getResources().getString(R.string.gpu_vr2d_fps), String.valueOf(gpuVr2dFps));
				
				if(!isViewMoved){
					moveView(mapStrings);
					isViewMoved = true;
				}
			}
		}
	}
	
	private VideoGamePlayer.GameEvent mGameCallBack = new VideoGamePlayer.GameEvent() {
		private int decoderFps;
		private int gpuFps;
		private boolean result = true;
		@Override
		public void onEvent(int event, int arg0, int arg1) {
			// TODO Auto-generated method stub
			synchronized (this) {

				if (event == VideoGamePlayer.GAME_EVENT_FINISHED) {
					MyLog.e(TAG, "finished");
					mPlayer.PlayTestStop();
					saveResult(decoderFps,gpuFps,result);
					boolean vrMode = getVrMode();
					if(vrMode){
						finish();
						PageJumpUtil.JumpToRealGameForTest(VrRealTestActivity.this, !vrMode, true,getIsFromSetting());
					}
				}
				if (event == VideoGamePlayer.GAME_EVENT_ERROR){
					MyLog.e(TAG, "Error");
					result = false;
				}
				if (event == VideoGamePlayer.GAME_EVENT_SPEED_FPS){
					decoderFps = ((arg0>>16) & 0xffff);
					gpuFps = (arg0) & 0xffff;
					MyLog.e(TAG, "decoder fps="+decoderFps+", GPU fps="+gpuFps);
				}
			}
		}

	};
	
	
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MyLog.e(TAG, "onPause");
		mIsPaused = true;
		if (mIsWorking)
			mPlayer.stopActionPlay();
		
		mapStrings.clear();
//		setDetectResult(stringBuffer);
	}

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MyLog.e(TAG, "onResume");
		if (mIsPaused && mIsWorking)
			mPlayer.PlayAction();
		
	}
	
	@Override
	protected synchronized void onDestroy() {
		super.onDestroy();
		MyLog.e(TAG, "onDestroy");
		mPlayer.releaseActionPlay();
	}
}
